<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MallShoplog extends BaseModel
{
    protected $table = 'mall_shoplog';
    protected $primaryKey = 'log_id';
    public $timestamps = false;

    public function mallGoods()
    {
        return $this->belongsTo(MallGoods::class, 'goods_id', 'goods_id');
    }

    public function statisticsGoods($input = [])
    {
        $limit = $input['limit'] ?? 10;
        $page = $input['page'] ?? 1;
        $startTime = $input['start_date'] ?? '';
        $endTime = $input['end_date'] ?? '';
        $goodsType = $input['goods_type'] ?: 0;
        $type = $input['type'] ?? 'd';
        $format = '%Y-%m-%d';
        if ($type == 'd') {
            $format = '%Y-%m-%d';
        } else if ($type == 'm') {
            $format = '%Y-%m';
            if (!empty($startTime)) {
                $startTime = substr($startTime, 0, 7);
            }
            if (!empty($endtime)) {
                $endTime = substr($endTime, 0, 7);
            }
        }

        $where = '';
        if (!empty($startTime)) {
            $where .= ' and \'' . $startTime . '\'<=FROM_UNIXTIME(a.create_timestamp,\'' . $format . '\')';
        }
        if (!empty($endTime)) {
            $where .= ' and \'' . $endTime . '\'>=FROM_UNIXTIME(a.create_timestamp,\'' . $format . '\')';
        }

        if ($goodsType) {
            $where .= "and b.goods_type={$goodsType}";
        }
        $sql = "SELECT FROM_UNIXTIME(a.create_timestamp,'{$format}') as ctime,COUNT(*) as count,
 a.goods_id,a.goods_name,SUM(price) as price_sum,b.goods_type
FROM zb_mall_shoplog as a LEFT JOIN zb_mall_goods as b on a.goods_id=b.goods_id
where 1=1 {$where} group BY a.goods_id,a.goods_name,FROM_UNIXTIME(a.create_timestamp,'{$format}') ORDER BY ctime desc ,price_sum desc";
        $data = DB::connection('api')->select($sql);
        $total = count($data);
        $list = collect($data)->forPage($page, $limit)->values()->toArray();
        return ['data' => $list, 'total' => $total];
    }

    public function getShopLogList($input)
    {
        $limit = $input['limit'] ?? 30;
        $uuid = $input['uuid'];
        $gift_id = $input['goods_id'];
        $begin_date = $input['begin_date'] != '' ? strtotime($input['begin_date']) : '';
        $end_date = $input['end_date'] != '' ? strtotime($input['end_date']) : '';


        $where = [];
        if($uuid != '') $where[] = ['uuid', '=', "{$uuid}"];
        if($gift_id != '') $where[] = [$this->table.'.goods_id', '=', $gift_id];
        if($begin_date && $end_date) {
            $where[] = [$this->table.'.create_timestamp', '>=', $begin_date];
            $where[] = [$this->table.'.create_timestamp', '<=', $end_date];
        }
        if($begin_date && !$end_date) $where[] = [$this->table.'.create_timestamp', '>=', $begin_date];
        if(!$begin_date && $end_date) $where[] = [$this->table.'.create_timestamp', '<=', $end_date];

        $res = DB::connection($this->connection)
            ->table($this->table)
            ->where($where)
            ->leftJoin('user', $this->table.'.to_user_id', '=', 'user.user_id')
            ->select('nickname', 'uuid', 'goods_id', 'goods_name', 'time_limit', $this->table.'.create_time','user.user_id')
            ->orderBy($this->table.'.create_timestamp', 'desc')
            ->paginate($limit);
        $res = $this->objToArray($res);
        foreach ($res['data'] as $k => $v) {
            $propNum = DB::connection($this->connection)->table('mall_user_goods')
                ->where('goods_id', '=', $v['goods_id'])
                ->where('user_id','=',$v['user_id'])
                ->select('end_timestamp')
                ->first();
            $res['data'][$k]['end'] = empty($propNum->end_timestamp) ? '' : date('Y-m-d H:i:s', $propNum->end_timestamp);
        }

        return $res;
    }


}
